package cc.rengu.igas.channel.esb.biz.service.impl;

import cc.rengu.igas.channel.esb.common.constant.EsbServiceConstant;
import cc.rengu.igas.channel.esb.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.esb.facade.BankCoreService;
import cc.rengu.igas.channel.esb.facade.request.*;
import cc.rengu.igas.channel.esb.facade.response.*;
import cc.rengu.igas.channel.esb.facade.result.Result;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.DubboResult;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;

/**
 * Created by wangcheng
 * 核心ESB服务实现
 *
 * @author wangcheng
 */
public class BankCoreServiceImpl implements BankCoreService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public Result<CoreAcctBalanceQueryResponse> coreAcctBalanceQuery(CoreAcctBalanceQueryRequest coreAcctBalanceQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreAcctBalanceQueryResponse> result = new Result<>();
        CoreAcctBalanceQueryResponse coreAcctBalanceQueryResponse = new CoreAcctBalanceQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreAcctBalanceQueryRequest, EsbServiceConstant.CORE_ACCT_BALANCE_QUERY_SERVICE, CoreAcctBalanceQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreAcctBalanceQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡账户余额查询失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreAcctBalanceQueryResponse.setRespCode(((BizException) e).getCode());
                coreAcctBalanceQueryResponse.setRespDesc(e.getMessage());
            } else {
                coreAcctBalanceQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreAcctBalanceQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreConsumeResponse> coreConsume(CoreConsumeRequest coreConsumeRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreConsumeResponse> result = new Result<>();
        CoreConsumeResponse coreConsumeResponse = new CoreConsumeResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreConsumeRequest, EsbServiceConstant.CORE_CONSUME_SERVICE, CoreConsumeResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreConsumeResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡消费失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreConsumeResponse.setRespCode(((BizException) e).getCode());
                coreConsumeResponse.setRespDesc(e.getMessage());
            } else {
                coreConsumeResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreConsumeResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreConsumeResvalResponse> coreConsumeRevsal(CoreConsumeResvalRequest coreConsumeResvalRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreConsumeResvalResponse> result = new Result<>();
        CoreConsumeResvalResponse coreConsumeResvalResponse = new CoreConsumeResvalResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreConsumeResvalRequest, EsbServiceConstant.CORE_CONSUME_REVSAL_SERVICE, CoreConsumeResvalResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreConsumeResvalResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡消费冲正失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreConsumeResvalResponse.setRespCode(((BizException) e).getCode());
                coreConsumeResvalResponse.setRespDesc(e.getMessage());
            } else {
                coreConsumeResvalResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreConsumeResvalResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreConsumeCancelResponse> coreConsumeCancel(CoreConsumeCancelRequest coreConsumeCancelRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreConsumeCancelResponse> result = new Result<>();
        CoreConsumeCancelResponse coreConsumeCancelResponse = new CoreConsumeCancelResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreConsumeCancelRequest, EsbServiceConstant.CORE_CONSUME_CANCEL_SERVICE, CoreConsumeCancelResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreConsumeCancelResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡消费撤销失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreConsumeCancelResponse.setRespCode(((BizException) e).getCode());
                coreConsumeCancelResponse.setRespDesc(e.getMessage());
            } else {
                coreConsumeCancelResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreConsumeCancelResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreConsumeCancelResvalResponse> coreConsumeCancelRevsal(CoreConsumeCancelResvalRequest coreConsumeCancelResvalRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreConsumeCancelResvalResponse> result = new Result<>();
        CoreConsumeCancelResvalResponse coreConsumeCancelResvalResponse = new CoreConsumeCancelResvalResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreConsumeCancelResvalRequest, EsbServiceConstant.CORE_CONSUME_CANCEL_REVSAL_SERVICE, CoreConsumeCancelResvalResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreConsumeCancelResvalResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡消费撤销冲正失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreConsumeCancelResvalResponse.setRespCode(((BizException) e).getCode());
                coreConsumeCancelResvalResponse.setRespDesc(e.getMessage());
            } else {
                coreConsumeCancelResvalResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreConsumeCancelResvalResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreConsumeRefundResponse> coreConsumeRefund(CoreConsumeRefundRequest coreConsumeRefundRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreConsumeRefundResponse> result = new Result<>();
        CoreConsumeRefundResponse coreConsumeRefundResponse = new CoreConsumeRefundResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreConsumeRefundRequest, EsbServiceConstant.CORE_CONSUME_REFUND_SERVICE, CoreConsumeRefundResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreConsumeRefundResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("银行卡消费退货失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreConsumeRefundResponse.setRespCode(((BizException) e).getCode());
                coreConsumeRefundResponse.setRespDesc(e.getMessage());
            } else {
                coreConsumeRefundResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreConsumeRefundResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<D0RealTimeEntryToBankAcctResponse> d0RealTimeEntryToBankAcct(D0RealTimeEntryToBankAcctRequest d0RealTimeEntryToBankAcctRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<D0RealTimeEntryToBankAcctResponse> result = new Result<>();
        D0RealTimeEntryToBankAcctResponse d0RealTimeEntryToBankAcctResponse = new D0RealTimeEntryToBankAcctResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(d0RealTimeEntryToBankAcctRequest, EsbServiceConstant.D0_REAL_TIME_ENTRY_TO_BANK_ACCT_SERVICE, D0RealTimeEntryToBankAcctResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((D0RealTimeEntryToBankAcctResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("商户实时入账失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                d0RealTimeEntryToBankAcctResponse.setRespCode(((BizException) e).getCode());
                d0RealTimeEntryToBankAcctResponse.setRespDesc(e.getMessage());
            } else {
                d0RealTimeEntryToBankAcctResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                d0RealTimeEntryToBankAcctResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreGeneralTransQueryResponse> coreGeneralTransQuery(CoreGeneralTransQueryRequest coreGeneralTransQueryRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreGeneralTransQueryResponse> result = new Result<>();
        CoreGeneralTransQueryResponse coreGeneralTransQueryResponse = new CoreGeneralTransQueryResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreGeneralTransQueryResponse, EsbServiceConstant.CORE_GENERAL_TRANS_QUERY_SERVICE, CoreGeneralTransQueryResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreGeneralTransQueryResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("通用交易状态查证失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreGeneralTransQueryResponse.setRespCode(((BizException) e).getCode());
                coreGeneralTransQueryResponse.setRespDesc(e.getMessage());
            } else {
                coreGeneralTransQueryResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreGeneralTransQueryResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreGeneralAcctTransferResponse> coreGeneralAcctTransfer(CoreGeneralAcctTransferRequest coreGeneralAcctTransferRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreGeneralAcctTransferResponse> result = new Result<>();
        CoreGeneralAcctTransferResponse coreGeneralAcctTransferResponse = new CoreGeneralAcctTransferResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreGeneralAcctTransferRequest, EsbServiceConstant.CORE_GENERAL_ACCT_TRANSFER_SERVICE, CoreGeneralAcctTransferResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreGeneralAcctTransferResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("通用交易转账失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreGeneralAcctTransferResponse.setRespCode(((BizException) e).getCode());
                coreGeneralAcctTransferResponse.setRespDesc(e.getMessage());
            } else {
                coreGeneralAcctTransferResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreGeneralAcctTransferResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }

    @Override
    public Result<CoreGeneralAcctRevsalResponse> coreGeneralAcctRevsal(CoreGeneralAcctRevsalRequest coreGeneralAcctRevsalRequest) {
        DubboService dubboService = new DubboServiceImpl();
        Result<CoreGeneralAcctRevsalResponse> result = new Result<>();
        CoreGeneralAcctRevsalResponse coreGeneralAcctRevsalResponse = new CoreGeneralAcctRevsalResponse();
        try {
            DubboResult dubboResult = dubboService.callAtomService(coreGeneralAcctRevsalRequest, EsbServiceConstant.CORE_GENERAL_ACCT_REVSAL_SERVICE, CoreGeneralAcctRevsalResponse.class);
            result.setSuccess(dubboResult.isSuccess());
            result.setResult((CoreGeneralAcctRevsalResponse) dubboResult.getResult());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("通用交易冲正失败:<{}>", byteArrayOutputStream.toString());
            result.setSuccess(false);
            if (e instanceof BizException) {
                coreGeneralAcctRevsalResponse.setRespCode(((BizException) e).getCode());
                coreGeneralAcctRevsalResponse.setRespDesc(e.getMessage());
            } else {
                coreGeneralAcctRevsalResponse.setRespCode(RespCodeEnum.SYSTEM_ERROR.getRespCode());
                coreGeneralAcctRevsalResponse.setRespDesc(RespCodeEnum.SYSTEM_ERROR.getRespDesc());
            }
        }
        return result;
    }
}
